package us.mooresr.webtroopmanager.client.activity;

import java.util.List;
import java.util.Set;

import us.mooresr.webtroopmanager.client.ClientFactory;
import us.mooresr.webtroopmanager.client.TroopManagerService;
import us.mooresr.webtroopmanager.client.TroopManagerServiceAsync;
import us.mooresr.webtroopmanager.client.events.UserAddedEvent;
import us.mooresr.webtroopmanager.client.place.EditUserPlace;
import us.mooresr.webtroopmanager.client.ui.EditUserView;
import us.mooresr.webtroopmanager.shared.PersonSummary;
import us.mooresr.webtroopmanager.shared.Role;
import us.mooresr.webtroopmanager.shared.User;
import us.mooresr.webtroopmanager.shared.UserSummary;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class EditUserActivity extends MyActivity implements EditUserView.Presenter {

	public EditUserActivity(Place place, ClientFactory clientFactory) {
		super(place, clientFactory);
		
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		super.start(panel, eventBus);
		
		EditUserView editUserView = clientFactory.getEditUserView();
		editUserView.setPresenter(this);
		loadRoles();
		// NOT loading user data here.  It's done in the onSuccess from loading
		// roles, so we know for sure the roles are loaded before we load the user data
		// loadUser(((EditUserPlace)place).getUserId());
		panel.setWidget(editUserView.asWidget());
	}

	private void loadRoles() {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);

		
		AsyncCallback<List<Role>> callback = new AsyncCallback<List<Role>>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getEditUserView().setData(null);
				
			}
			
			@Override
			public void onSuccess(List<Role> result) {
				clientFactory.getEditUserView().setRoles(result);
				loadUser(((EditUserPlace)place).getUserId());
				
			}
		};
		
		troopManagerService.getAllRoles(callback);
		
	}

	@Override
	public void addUser(User user) {
		TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		
		AsyncCallback<UserSummary> callback = new AsyncCallback<UserSummary>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(UserSummary result) {
				
				clientFactory.getEventBus().fireEvent(new UserAddedEvent(result));

			}
		};
		
		troopManagerService.addUser(user, callback);
		
	}
	
	private void loadUser(Long userId) {
	TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		if (userId == null) {
			clientFactory.getEditUserView().setData(null);
			return;
		}
		
		AsyncCallback<User> callback = new AsyncCallback<User>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getEditUserView().setData(null);
				
			}
			
			@Override
			public void onSuccess(User result) {
				clientFactory.getEditUserView().setData(result);
				// Although loading the User also gave us his Person list,
				// that's a list of keys.  We need the PersonSummary objects
				// so that we can fill in the names on the view.
				loadPersonSummaries(result.getPersonIds());
				
			}
		};
		
		troopManagerService.getUser(userId, callback);
		
	}

	protected void loadPersonSummaries(Set<Long> personIds) {
	TroopManagerServiceAsync troopManagerService = (TroopManagerServiceAsync) GWT.create(TroopManagerService.class);
		
		AsyncCallback<List<PersonSummary>> callback = new AsyncCallback<List<PersonSummary>>() {
			@Override
			public void onFailure(Throwable caught) {
				//
				
			}
			
			@Override
			public void onSuccess(List<PersonSummary> result) {
				clientFactory.getEditUserView().setPeople(result);

				
			}
		};
		
		troopManagerService.getPeople(personIds, callback);	
		
	}



}
